﻿import com.dinmax.flash.common.collection.*
import com.dinmax.flash.gsa.communication.parameter.*
import com.dinmax.flash.gsa.games.gameSlot.*
import com.dinmax.flash.gsa.sgs.draw.spin.*
import com.dinmax.flash.gsa.sgs.bridge.slot.*
import com.dinmax.flash.gsa.sgs.game.*
import com.dinmax.flash.gsa.sgs.*

class SLOTBridge
implements SpinManagerListener{
	
	private var parent:MovieClip;
	private var spManager:SpinManager;
	
	private static var instance:SLOTBridge;
	
	public static function newInstance(parent:MovieClip, serial:String):Void {
		instance= new SLOTBridge(parent, serial);
	}; //fin función

	public static function getActive():SLOTBridge{
		return instance;
	};
	
	function SLOTBridge (parent:MovieClip,serial:String){
		this.parent=parent;
		createSpins(this);
		SLOTBridgeButtons.initButtons(parent, this);
		GameSlot.newInstance(this, serial);
		//SGS.getActive(); /* si llamo al SGS acá se rompe todo el script ¿? */
	};	

	public function getParent():MovieClip {
		return parent;
	}; //fin función
		
	
	//GAMESLOT 2 SWF
		public function GAMESLOT_2_SWF_setPosition(parameters:Parameter, noRound:Boolean):Void {
			setPosition(parameters, noRound);
			parent.mcSpin._visible=true;
			parent.mcSpin2._visible=true;
			parent.mcSpin3._visible=true;
		}; //fin función
		
		public function GAMESLOT_2_SWF_setRules(rules:Rules):Void {
			var deltaY:Number=115;
			var rulesX:Number=166;
			for(var i:Number=0; i < rules.getItems().length; i++){
				var deltaX:Number=0;
				
				if(rules.getItems()[i].getParameters().getItems()[0]){
				
				var mcRule:MovieClip=parent.createEmptyMovieClip("rule"+i, parent.getNextHighestDepth());
				for(var j:Number=0; j < 3; j++){
					
					if (rules.getItems()[i].getParameters().getItems()[j].getValue() < 0) {
						var sLink:String="Joker";
					} else {
						var nElement:Number=rules.getItems()[i].getParameters().getItems()[j].getValue();
						//trace("nElement "+nElement); 
						var sLink:String="element"+nElement;
					};
					var mcElement:MovieClip= mcRule.attachMovie(sLink, "mcElement"+j, mcRule.getNextHighestDepth());
					mcElement._xscale=20;
					mcElement._yscale=20;
					mcElement._x=deltaX;
					deltaX+= mcElement._width + 4;

				}
				mcRule.attachMovie("TokenMultiply", "mcTokenMultiply", mcRule.getNextHighestDepth());
				mcRule.mcTokenMultiply._x=deltaX+2;
				mcRule.mcTokenMultiply._y=-3;
				deltaX+=mcRule.mcTokenMultiply._width;
				mcRule.mcTokenMultiply.txt_multiply.text="x"+rules.getItems()[i].getPrize().getTokenMultiply();
				mcRule.mcTokenMultiply.txt_multiply2.text="x"+rules.getItems()[i].getPrize().getTokenMultiply();
				mcRule._x=rulesX;
				mcRule._y=deltaY;
				deltaY+= mcRule._height + 8;
				}
				
			};
		}; //fin función
		
		public function GAMESLOT_2_SWF_noMinBet():Void {
			marquee("noMinBet");
			lightsOff();
			SLOTBridgeButtons.unblockButtons(parent);
		}; //fin función
		
		public function GAMESLOT_2_SWF_setBet(betTokens:Number):Void {
			marquee("bet");
			if(betTokens<10){
				parent.mcMarquee.txt_bet.text="000"+betTokens;
			} else if (betTokens<100) {
				parent.mcMarquee.txt_bet.text="00"+betTokens;
			} else if (betTokens<1000){
				parent.mcMarquee.txt_bet.text="0"+betTokens;
			} else if (betTokens<10000){
				parent.mcMarquee.txt_bet.text=betTokens;
			};
		}; //fin función

		public function GAMESLOT_2_SWF_setTokens(tokens:Number):Void {
			trace("SLOTBridge.as | setTokens()= "+tokens);
			marquee("bet");
			if(tokens<10){
				parent.mcMarquee.txt_tokens.text="000"+tokens;
			} else if (tokens<100) {
				parent.mcMarquee.txt_tokens.text="00"+tokens;
			} else if (tokens<1000){
				parent.mcMarquee.txt_tokens.text="0"+tokens;
			} else if (tokens<10000){
				parent.mcMarquee.txt_tokens.text=tokens;
			};
		}; //fin función

		public function GAMESLOT_2_SWF_win(level:Number):Void {
			marquee("win");
			lightsOn(level);
			
			var cont:Number=0;
			var root:SLOTBridge=this;
			parent.onEnterFrame=function(){
				if(cont==52){
					delete this.onEnterFrame;
					root.SWF_2_GAMESLOT_roundEnd(true);
					root.lightsOff();
				} else {
					cont++;
				};
			};
			
		}; //fin función
		

	//
	
	//SWF 2 GAMESLOT
		public function SWF_2_GAMESLOT_gameLogOut():Void {
			GameSlot.getActive().gameLogOut();
		}; //fin función
		
		public function SWF_2_GAMESLOT_play(tokens:Number):Void {
			GameSlot.getActive().play(tokens);
		}; //fin función

		public function SWF_2_GAMESLOT_roundEnd(win:Boolean):Void {
			GameSlot.getActive().roundEnd(win);
		}; //fin función

		public function SWF_2_GAMESLOT_addBet(tokens:Number, arg:String):Void {
			GameSlot.getActive().addBet(tokens, arg);
		}; //fin función

		public function SWF_2_GAMESLOT_deleteBet():Void {
			GameSlot.getActive().deleteBet();
		}; //fin función
		
	//

	public function setPosition(position:Collection, noRound:Boolean):Void {
		spManager.setPosition(position, noRound);
	}; //fin función
	
	public function spinsRoundsEnds(id:Number):Void {
		lightsOff();
		SLOTBridgeButtons.unblockButtons(parent);
		SWF_2_GAMESLOT_roundEnd();
	}; //fin función

	public function play():Void {
		//acá le tengo que decir al GAMESLOT que haga play() con el bet que haya;
		//trace("SLOTBridge.as | me hacen un play");
		lightsOn();
		marquee("play");
		SLOTBridgeSounds.play(parent);
		SLOTBridgeButtons.blockButtons(parent);
		SWF_2_GAMESLOT_play(0);
	}; //fin función
	
	private function createSpins(root:SLOTBridge):Void {
				
		parent.mcSpin.stop();
		parent.mcSpin._visible=false;
		parent.mcSpin2.stop();
		parent.mcSpin2._visible=false;
		parent.mcSpin3.stop();
		parent.mcSpin3._visible=false;
		
		parent.palanca.onPress=function(){
			//trace("tocaron la palanca");
			root.play();
		};

		var spin1:Spin= new Spin(1, parent.mcSpin);
		spin1.setFrameStep(25);

		var spin2:Spin= new Spin(2, parent.mcSpin2);
		spin2.setFrameStep(25);

		var spin3:Spin= new Spin(3, parent.mcSpin3);
		spin3.setFrameStep(25);

		spManager=new SpinManager(1);
		spManager.addListener(this);
		spManager.addSpin(spin1.getId(), spin1);
		spManager.addSpin(spin2.getId(), spin2);
		spManager.addSpin(spin3.getId(), spin3);
		
	}; //fin función
	
	private function createButtons(root:SLOTBridge){
		
	};

	private function lightsOn(level:Number):Void {
		//trace(parent.mcLights);
		if(level >= 0){
		parent.mcLights.lightsWin(level);
		} else {
		parent.mcLights.lightsOn();
		}
	};

	private function lightsOff():Void {
		parent.mcLights.lightsOff();
	};
	
	private function marquee(frame:String):Void{
		if (frame=="win")
		{
				SLOTBridgeSounds.win(parent);
		};

		parent.mcMarquee.gotoAndStop(frame);
	};
	

}; // fin de clase